package pers.cz.thread;

import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pers.cz.mapper.Mapper;
import pers.cz.session.ContextHolder;
import pers.cz.spring.SpringContextUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: QueryWorker
 * @author: Cheng Zhi
 * @create: 2022-05-04 11:49
 **/
public class QueryWorker extends Worker {

    /**
     * 查询分表
     * @param input
     * @return
     */
    public Map<SqlSession, Object> handle(Object input) {
        SqlSessionFactory sqlSessionFactory = SpringContextUtil.getBean(SqlSessionFactory.class);
        SqlSession sqlSession = sqlSessionFactory.openSession();  // 1
        Cursor<Object> objects = null;
        try {
            Mapper bean = (Mapper)sqlSession.getMapper((Class)input);
            objects = bean.selectAllIterator();
/*        Mapper bean = (Mapper) SpringContextUtil.getBean((Class)input);
        // 通过反射执行
        Object o = bean.selectAll();*/

        } finally {
            //sqlSession.close();
        }
        Map<SqlSession, Object> result = new HashMap<>();
        result.put(sqlSession, objects);
        return result;
    }
}
